package com.jsmtr.rms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsmtr.rms.entity.LayuiDataTableResultBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jsmtr.rms.entity.BaseEvaluationItem;
import com.jsmtr.rms.entity.BaseEvaluationTable;
import com.jsmtr.rms.entity.vo.BaseEvaluations;
import com.jsmtr.rms.service.IBaseEvaluationService;
import com.jsmtr.rms.service.IUtilService;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/BaseEvaluation")
public class BaseEvaluationController {

    @Resource
    private IUtilService utilService;

    @Resource
    private IBaseEvaluationService baseEvaluationService;

    //主界面
    @RequestMapping("/")
    public String Index(Model model) {
        return "/baseevaluation/index";
    }

    @RequestMapping("/getlists")
    public void getLists(HttpServletRequest request, HttpServletResponse response) {
        try {
            StringBuilder result = new StringBuilder();
            List<BaseEvaluationTable> tables = baseEvaluationService.getTables();
            int num = 1;
            for (BaseEvaluationTable table : tables) {
                result.append("<tr><td><input type=\"checkbox\" id=\"cb_" + table.getId() + "\" /></td>");
                result.append("<td>" + num + "</td>");
                result.append("<td>" + table.getName() + "</td></tr>");
                num++;
            }
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='3'>读取数据失败</td></tr>", response);
        }
    }

    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping("/getlistsLayuiJson")
    public LayuiDataTableResultBean getlistsLayuiJson(HttpServletRequest request, HttpServletResponse response) {
        try {
            int page = Integer.parseInt(request.getParameter("page"));
            int limit = Integer.parseInt(request.getParameter("limit"));

            List<BaseEvaluationTable> tables = baseEvaluationService.getTables();
            JSONArray jsonArray = new JSONArray();
            int num = 1;
            for (BaseEvaluationTable table : tables) {
                JSONObject jsonObject = JSONObject.fromObject(table);
                jsonObject.put("ids","cb_" + table.getId());
                jsonObject.put("id",num);
                jsonObject.put("name",table.getName());

                jsonArray.add(jsonObject);
                num++;
            }
            LayuiDataTableResultBean result = new LayuiDataTableResultBean();
            result.setCode(0);
            result.setMsg("success");
            result.setCount(jsonArray.size());
            result.setData(jsonArray);

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            LayuiDataTableResultBean result = new LayuiDataTableResultBean();
            result.setCount(-1);
            result.setMsg("err");
            return result;
        }
    }

    //添加
    @RequestMapping("/add")
    public String Add() {
        return "/baseevaluation/add";
    }

    //添加
    @SuppressWarnings({"unchecked", "rawtypes"})
    @RequestMapping(value = "/AddAction", method = RequestMethod.POST)
    public void AddAction(HttpServletRequest request, HttpServletResponse response, BaseEvaluations evaluation) {
        try {
            List list = new ArrayList();
            BaseEvaluationTable table = new BaseEvaluationTable();
            String tid = UUID.randomUUID().toString();
            table.setName(evaluation.getName());
            table.setId(tid);
            list.add(table);
            int weight = 0;
            for (BaseEvaluationItem item : evaluation.getEvaluationItems()) {
                item.setId(UUID.randomUUID().toString());
                item.setTid(tid);
                item.setTname(evaluation.getName());
                list.add(item);
                weight += item.getAllpoint();
            }
            if (weight == 100) {
                utilService.addList(list);
                utilService.printJsonResult(true, "操作成功！", "no", response);
            } else {
                utilService.printJsonResult(false, "分数之和必须等于100！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //修改
    @RequestMapping("/edit")
    public String Edit(HttpServletRequest request, Model model) {
        try {
            String id = request.getParameter("id").trim();
            BaseEvaluationTable table = baseEvaluationService.getTable(id);
            model.addAttribute("table", table);
            List<BaseEvaluationItem> items = baseEvaluationService.getBaseEvaluationItems(id);
            model.addAttribute("items", items);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/baseevaluation/edit";
    }

    //修改
    @RequestMapping(value = "/EditAction", method = RequestMethod.POST)
    public void EditAction(HttpServletRequest request, HttpServletResponse response, BaseEvaluations evaluation) {
        try {
            String id = request.getParameter("id").trim();
            //修改
            BaseEvaluationTable table = baseEvaluationService.getTable(id);
            table.setName(evaluation.getName());
            //删除
            List<BaseEvaluationItem> items = baseEvaluationService.getBaseEvaluationItems(id);
            //添加
            int weight = 0;
            for (BaseEvaluationItem item : evaluation.getEvaluationItems()) {
                item.setId(UUID.randomUUID().toString());
                item.setTid(id);
                item.setTname(evaluation.getName());
                weight += item.getAllpoint();
            }
            if (weight == 100) {
                utilService.editList(table, items, evaluation.getEvaluationItems());
                utilService.printJsonResult(true, "操作成功！", "no", response);
            } else {
                utilService.printJsonResult(false, "评分权重之和必须等于100！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //删除
    @SuppressWarnings({"unchecked", "rawtypes"})
    @RequestMapping(value = "/DelAction", method = RequestMethod.POST)
    public void DelAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String id = request.getParameter("id").trim();
            List list = new ArrayList();
            BaseEvaluationTable table = baseEvaluationService.getTable(id);
            list.add(table);
            List<BaseEvaluationItem> items = baseEvaluationService.getBaseEvaluationItems(id);
            list.addAll(items);

            utilService.delList(list);
            utilService.printJsonResult(true, "操作成功！", "no", response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //查看
    @RequestMapping("/view")
    public String View(HttpServletRequest request, Model model) {
        try {
            String id = request.getParameter("id").trim();
            BaseEvaluationTable table = baseEvaluationService.getTable(id);
            model.addAttribute("table", table);
            List<BaseEvaluationItem> items = baseEvaluationService.getBaseEvaluationItems(id);
            model.addAttribute("items", items);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/baseevaluation/view";
    }

}
